package com.iamteer.mail;

import java.io.File;
import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.junit.Test;

import com.sun.net.ssl.internal.ssl.Provider;

/**
 * 发送带图片邮件
 * @author Chuck
 *
 */
public class AppSendMailWithAtta {
	private static Properties prop;
	private static Session session;
	private static MimeMessage message;
	private String username = "learnjavamail";
	private String password = "root123456";
	static{
	       //设置SSL连接、邮件环境
        Security.addProvider(new Provider());  
        String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";  
        prop = new Properties();
        
        prop.setProperty("mail.smtp.host", "smtp.zoho.com");
        prop.setProperty("mail.transport.protocol", "smtp");
        prop.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
        prop.setProperty("mail.smtp.socketFactory.fallback", "false");
        prop.setProperty("mail.smtp.port", "465");
        prop.setProperty("mail.smtp.socketFactory.port", "465");
        prop.setProperty("mail.smtp.auth", "true");
        
        // 创建一个邮件会话
        session = Session.getDefaultInstance(prop);
        // 创建邮件体对象
        message = new MimeMessage(session);
	}
	
	/**
	 * 带图片及附件的邮件
	 * @throws Exception
	 */
	@Test
	public void testSend() throws Exception{
		// 设置邮件体参数
		message.setSubject("带图片及附件的邮件");
		message.setSentDate(new Date());
		message.setSender(new InternetAddress("learnjavamail@zoho.com"));
		message.setRecipient(RecipientType.TO, new InternetAddress("lch0821@foxmail.com"));
		
		/****************主体内容****************/
		// 构建一个总的邮件块，并设置到邮件对象中
		MimeMultipart mixed = new MimeMultipart("mixed");
		message.setContent(mixed);
		// 左侧：（文本+图片资源）
		MimeBodyPart left = new MimeBodyPart();
		// 右侧：附件
		MimeBodyPart right = new MimeBodyPart();
		// 设置到总邮件块中
		mixed.addBodyPart(left);
		mixed.addBodyPart(right);

		/*****附件******/
		String attr_name = "Attachment.zip";
//		attr_name = new String(attr_name.getBytes("gbk"), "utf-8");
//		System.out.println(attr_name);
		String attr_path = AppSendMailWithAtta.class.getResource(attr_name).getPath();
		DataSource attr_ds = new FileDataSource(new File(attr_path));
		DataHandler attr_handler = new DataHandler(attr_ds);
		right.setDataHandler(attr_handler);
		right.setFileName(attr_name);
		
		// 构建多功能邮件块并设置到左侧：
		MimeMultipart related = new MimeMultipart("related");
		left.setContent(related);
		// 构建多功能邮件块内容：文本+图片
		MimeBodyPart content = new MimeBodyPart();		// 文本内容
		MimeBodyPart resource = new MimeBodyPart();		// 图片资源
		
		// 设置具体内容：1. 资源（图片）
		String path = AppSendMailWithAtta.class.getResource("test.jpg").getPath();
		FileDataSource ds = new FileDataSource(new File(path));
		DataHandler handler = new DataHandler(ds);
		resource.setDataHandler(handler);
		resource.setContentID("test.jpg");	// 设置资源名称，给外键引用
		
		// 设置具体内容：2. 文本
		content.setContent("<img src='cid:test.jpg'/> <br/> 哈哈哈哈哈哈", "text/html;charset=UTF-8");
		
		related.addBodyPart(content);
		related.addBodyPart(resource);
		
		// 发送
		Transport trans = session.getTransport();
		trans.connect(username, password);
		trans.sendMessage(message, message.getAllRecipients());
		trans.close();
	}
}
